#!/usr/bin/env python 

import inkex
import simplestyle, sys
import andengineutils

class AndEngine(inkex.Effect):
    def __init__(self):
        """
        Constructor.
        Defines the "--what" option of a script.
        """
        # Call the base class constructor.
        inkex.Effect.__init__(self)
        
        # Define string option "--what" with "-w" shortcut and default value "World".
        self.OptionParser.add_option("--tilecols", action="store", type="int", dest="tilecols", default=0, help="tile cols")
        self.OptionParser.add_option("--tilerows", action="store", type="int", dest="tilerows", default=0, help="tile rows")
        self.OptionParser.add_option("--starttile", action="store", type="int", dest="starttile", default=1, help="start tile")
        self.OptionParser.add_option("--hasanimation", action="store", type="inkbool", dest="hasanimation", default=False, help="has animation")
        self.OptionParser.add_option("--animationstarttile", action="store", type="int", dest="animationstarttile", default=0, help="animation starttile")
        self.OptionParser.add_option("--animationendtile", action="store", type="int", dest="animationendtile", default=0, help="animation endtile")
        self.OptionParser.add_option("--animationloop", action="store", type="inkbool", dest="animationloop", default=False, help="animation loop")
        self.OptionParser.add_option("--animationduration", action="store", type="string", dest="animationduration", default="", help="animation duration")
    
    def effect(self):
        for id, node in self.selected.iteritems():
            andengineutils.creatOrDelete("andengine-tilecols", node, self.options.tilecols, 1)
            andengineutils.creatOrDelete("andengine-tilerows", node, self.options.tilerows, 1)
            andengineutils.creatOrDelete("andengine-starttile", node, self.options.starttile, 0)
            
            if (self.options.hasanimation):
                andengineutils.creatOrDelete("andengine-has-animation", node, self.options.hasanimation, False)
                andengineutils.creatOrDelete("andengine-animation-starttile", node, self.options.animationstarttile, 0)
                andengineutils.creatOrDelete("andengine-animation-endtile", node, self.options.animationendtile, 0)
                andengineutils.creatOrDelete("andengine-animation-loop", node, self.options.animationloop, False)
                andengineutils.creatOrDelete("andengine-animation-duration", node, self.options.animationduration, "")
            else:
                andengineutils.delAttribute("andengine-has-animation", node)
                andengineutils.delAttribute("andengine-animation-starttile", node)
                andengineutils.delAttribute("andengine-animation-endtile", node)
                andengineutils.delAttribute("andengine-animation-loop", node)
                andengineutils.delAttribute("andengine-animation-duration", node)
        
if __name__ == '__main__':
    e = AndEngine()
    e.affect()